package com.zyf.project.kvdb.protocol.proto.wal.impl;

import com.zyf.project.binaryproto.common.BinaryProtocol;
import com.zyf.project.kvdb.protocol.proto.wal.Entity;
import com.zyf.project.kvdb.wal.walinf.WalDataCoder;

/**
 * <h1>实体编码器</h1>
 * <p>实现了 {@link WalDataCoder} 接口</p>
 * @author Zhou Yifan
 */
public class EntityCoder implements WalDataCoder<Entity> {
    /**
     * 数据编码
     *
     * @param data 待处理数据
     * @return byte[] 字节码
     */
    @Override
    public byte[] encode(Entity data) {
        return BinaryProtocol.encode(data, Entity.class);
    }

    /**
     * 数据解码
     *
     * @param data byte[] 待处理数据的字节码
     * @return 对应的数据
     */
    @Override
    public Entity decode(byte[] data) {
        return BinaryProtocol.decode(data);
    }

    public static EntityCoder getInstance() {
        return WalEntityCoderHolder.INSTANCE;
    }

    private static class WalEntityCoderHolder {
        private static final EntityCoder INSTANCE = new EntityCoder();
    }
}
